package com.luzhikun.myshopping.controller;

import com.luzhikun.myshopping.dao.ProductDao;
import com.luzhikun.myshopping.dao.impl.ProductDaoImpl;
import com.luzhikun.myshopping.domaim.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
@WebServlet("/product")
public class ProductServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if (action == null){
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
        }
        if ("delete".equals(action)){
            deletePro(req,resp);
        }
        if ("select".equals(action)){
            selectPro(req,resp);
        }
        if ("add".equals(action)){
            addPro(req,resp);
        }

    }

    /*
     * @Author liu-miss
     * @Description 添加商品
     * @Date 10:28 2021/7/16
     * @Param [req, resp]
     * @return void
     **/
    private void addPro(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");

        String pid = req.getParameter("pid");
        String pname = req.getParameter("pname");
        String pdate = req.getParameter("pdate");
        String pfactory = req.getParameter("pfactory");

        if (pid !=null && pname!=null && pdate!=null && pfactory!=null && !"".equals(pid)
                && !"".equals(pname) && !"".equals(pdate) && !"".equals(pfactory) ) {

            ProductDao productDao = new ProductDaoImpl();
            Product product = new Product(pid, pname, pdate, pfactory);
            boolean b = productDao.addProduct(product);
            String msg = "";
            if (b) {
                msg = "商品添加成功";
                req.setAttribute("msg", msg);
                req.getRequestDispatcher("/show").forward(req, resp);
            } else {
                msg = "商品添加失败";
                req.setAttribute("msg", msg);
                req.getRequestDispatcher("#").forward(req, resp);
            }
        }
        }

    /*
     * @Author liu-miss
     * @Description 查看商品
     * @Date 10:28 2021/7/16
     * @Param [req, resp]
     * @return void
     **/
    private void selectPro(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {


        ProductDao productDao = new ProductDaoImpl();

        String pid = req.getParameter("pid");
        System.out.println(pid);

        Product product = productDao.selectOneProduct(pid);
        if (product!=null){
            System.out.println(product);
            req.setAttribute("product",product);
            req.getRequestDispatcher("selectOneProduct.jsp").forward(req,resp);
        }else {
            System.out.println("商品是空的");
        }

    }

    /*
     * @Author liu-miss
     * @Description 删除商品
     * @Date 10:28 2021/7/16
     * @Param [req, resp]
     * @return void
     **/
    private void deletePro(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String pid = req.getParameter("pid");
        ProductDao productDao = new ProductDaoImpl();
        boolean b = productDao.delectProductById(pid);
        if (b){
            writer.println("删除商品成功");
        }else {
            writer.println("删除商品失败");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
